  layui.config({
        base: './../../layui/lay/modules/'
    });


layui.use(['form','layer','jquery','notice'],function(){
	var notice = layui.notice;
	datas= {
			  password : "",
			  userName:"",
			  tokendata:""
		  }
	const formvue=
	new Vue({
		  el: '#login',
		  data:datas,
		  methods: {
			  token:function(f){
				  $(f).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");

				 if(this.password!=""&&this.userName!=""){
					 Vue.http.headers.common.Authorization=null
					 this.$http.post(global_url+'/login', {name: this.userName,pwd:this.password})
						 .then(response => {
								if(response.body.code!=-1){
											notice.init({
												type: "default",
												autoClose: true,
												title: "登录成功",
												desktop: true,
											});
											this.tokendata=response.body.token;
											this.isadmin()
										
							
								}else{
											notice.init({
												type: "default",
												autoClose: true,
												title: response.body.datas.reason,
												desktop: true,
											});
									
								}
									$(f).text("登录").attr("disabled","disabled").removeClass("layui-disabled");
											$(f).text("登录").removeAttr("disabled");
				
						  }, response => {
							if(response.status==0){
								layer.msg("未响应！请稍后再试吧！");
							} else{
								layer.msg(response.body.msg);
							}
							
							$(f).text("登录").attr("disabled","disabled").removeClass("layui-disabled");
							$(f).text("登录").removeAttr("disabled");
					});



				 }
			  },
			  isadmin:function(){
				
				  Vue.http.headers.common.Authorization = 'Bearer '+this.tokendata;
				  this.$http.post(global_url+'/admin/isadmin')
				  	.then(
						response=>{
							window.localStorage.setItem('token', 'Bearer '+this.tokendata);
							window.localStorage.setItem('name', this.userName);
							window.localStorage.setItem('pwd', this.password);
							setTimeout(function(){
								window.location.href = "/backstage_management/index.html";
							},1000);
						
						},					
						response=>{
							console.log(response)
							if(!response.ok){
								layer.msg('不允许登入');
								
							}
						})
						
			  }
		  }
	})
	
	
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;

	
//     $(".loginBody .seraph").click(function(){
//         layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
//             time:5000
//         });
//     })

    //登录按钮
    form.on("submit(login)",function(data){
      

		formvue.token(this)

        return false;
    })

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
